package vn.vfriends.id.jpa.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author tuan@vfriends.vn
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "User.findByUsername", query = "SELECT u FROM User u WHERE u.username = :username"),
    @NamedQuery(name = "User.findByEmail", query = "SELECT u FROM User u WHERE u.email = :email"),
    @NamedQuery(name = "User.findByVerificationKey", query = "SELECT u FROM User u WHERE u.verificationKey = :key")})
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class User implements Serializable {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false)
    @XmlAttribute
    private Long id;
    
    @Size(max=50)
    @Column(length=50, unique=true, nullable=false)
    @XmlAttribute
    private String username;
    
    @Size(max=6)
    @Column(length=6)
    @XmlAttribute
    private String cif;
    
    @Size(max=50)
    @Column(length=50, unique=true, nullable=false)
    @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")
    @XmlAttribute
    private String email;
    
    @Size(max=50)
    @Column(length=50)
    private String password;
    
    @Size(max=100)
    @Column(length=100, name="FULL_NAME")
    @XmlAttribute
    private String fullName;
    
    @Column(name="ACTIVATED")
    @XmlAttribute
    private boolean activated;
    
    @Column(name="EXTERNAL")
    @XmlAttribute
    private boolean external;
    
    @Size(max=50)
    @Column(length=50, name="VERIFICATION_KEY")
    private String verificationKey;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="CREATED_DATE")
    @XmlAttribute
    private Date createdDate;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="MODIFIED_DATE")
    @XmlAttribute
    private Date modifiedDate;
    
    @ManyToMany(cascade= CascadeType.MERGE)
    @JoinTable(name = "USER_GROUP", joinColumns = @JoinColumn(name = "USER_ID"), 
            inverseJoinColumns = @JoinColumn(name = "GROUP_ID"))
    private List<Group> groups = new ArrayList<Group>();
    
    @PrePersist
    public void initTimeStamps() {
        if (createdDate == null) {
            createdDate = new Date();
        }
        modifiedDate = createdDate;
    }

    @PreUpdate
    public void updateTimeStamp() {
        modifiedDate = new Date();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public boolean isActivated() {
        return activated;
    }

    public void setActivated(boolean activated) {
        this.activated = activated;
    }

    public boolean isExternal() {
        return external;
    }

    public void setExternal(boolean external) {
        this.external = external;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getVerificationKey() {
        return verificationKey;
    }

    public void setVerificationKey(String verificationKey) {
        this.verificationKey = verificationKey;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public List<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }

    public String getCif() {
        return cif;
    }

    public void setCif(String cif) {
        this.cif = cif;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }
}
